package homework.level01model03.homework04;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentAdd {

    public static void add(ArrayList<Student> arrayList){

        Student instance = Student.getInstance();

        Scanner sc = new Scanner(System.in);
        //获取姓名学号年龄
        System.out.println("学号:");
        int id = sc.nextInt();
        System.out.println("姓名:");
        String name= sc.next();
        System.out.println("年龄:");
        int age = sc.nextInt();



        //System.out.println(arrayList);
        if (arrayList.size()==0){
            instance.setId(id);
            instance.setName(name);
            instance.setAge(age);
            arrayList.add(instance);
            System.out.println("插入成功");
        }else{
            int index = -1;
            for (Student student : arrayList) {
                //System.out.println("ok??");
                if (student.getId()==id){
                    index = 1;
                    System.out.println("该学生已存在不能插入");
                    break;
                }else{
                    System.out.println("可以插入");
                }
            }
            if (index == -1){
                instance.setId(id);
                instance.setName(name);
                instance.setAge(age);
                arrayList.add(instance);
                System.out.println("插入成功");
            }

        }
        //System.out.println("为何不执行");
        //遍历集合所有元素
        for (Student student : arrayList) {
            System.out.println(student);
        }




    }
}
